{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1b99e27f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01</th>\n",
       "      <td>0.088037</td>\n",
       "      <td>-1.471560</td>\n",
       "      <td>-0.037564</td>\n",
       "      <td>0.461296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.685722</td>\n",
       "      <td>-1.361501</td>\n",
       "      <td>0.541226</td>\n",
       "      <td>-1.300123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>0.441797</td>\n",
       "      <td>-1.124435</td>\n",
       "      <td>-1.628122</td>\n",
       "      <td>-0.923023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>1.634671</td>\n",
       "      <td>1.040169</td>\n",
       "      <td>0.207995</td>\n",
       "      <td>-0.332343</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05</th>\n",
       "      <td>-0.606727</td>\n",
       "      <td>-0.557548</td>\n",
       "      <td>-1.776780</td>\n",
       "      <td>0.299057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-06</th>\n",
       "      <td>1.846724</td>\n",
       "      <td>0.625768</td>\n",
       "      <td>-2.041737</td>\n",
       "      <td>0.859225</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2013-01-01  0.088037 -1.471560 -0.037564  0.461296\n",
       "2013-01-02 -0.685722 -1.361501  0.541226 -1.300123\n",
       "2013-01-03  0.441797 -1.124435 -1.628122 -0.923023\n",
       "2013-01-04  1.634671  1.040169  0.207995 -0.332343\n",
       "2013-01-05 -0.606727 -0.557548 -1.776780  0.299057\n",
       "2013-01-06  1.846724  0.625768 -2.041737  0.859225"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "dates = pd.date_range(\"20130101\", periods=6)\n",
    "df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list(\"ABCD\"))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e508f7ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01</th>\n",
       "      <td>0.088037</td>\n",
       "      <td>-1.471560</td>\n",
       "      <td>-0.037564</td>\n",
       "      <td>0.461296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.685722</td>\n",
       "      <td>-1.361501</td>\n",
       "      <td>0.541226</td>\n",
       "      <td>-1.300123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>0.441797</td>\n",
       "      <td>-1.124435</td>\n",
       "      <td>-1.628122</td>\n",
       "      <td>-0.923023</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2013-01-01  0.088037 -1.471560 -0.037564  0.461296\n",
       "2013-01-02 -0.685722 -1.361501  0.541226 -1.300123\n",
       "2013-01-03  0.441797 -1.124435 -1.628122 -0.923023"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2fbe3763",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "data = pd.read_excel(\"titanic3.xls\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1efd4bda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sex\n",
      "female    466\n",
      "male      843\n",
      "dtype: int64\n",
      "              Age\n",
      "Sex              \n",
      "female  28.687088\n",
      "male    30.585228\n"
     ]
    }
   ],
   "source": [
    "#获取字段sex的数据，并按照sex分组\n",
    "group_sex = data[[\"Sex\",\"Age\"]].groupby(by=\"Sex\")\n",
    "#使用size方法计算每组数量\n",
    "print(group_sex.size())\n",
    "#使用mean方法计算每组均值\n",
    "print(group_sex.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "647417f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   PassengerId  Survived  Pclass  \\\n",
      "0            1         0       3   \n",
      "1            2         1       1   \n",
      "2            3         1       3   \n",
      "3            4         1       1   \n",
      "4            5         0       3   \n",
      "\n",
      "                                                Name     Sex   Age  SibSp  \\\n",
      "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
      "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
      "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
      "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
      "4                           Allen, Mr. William Henry    male  35.0      0   \n",
      "\n",
      "   Parch            Ticket     Fare Cabin Embarked  \n",
      "0      0         A/5 21171   7.2500   NaN        S  \n",
      "1      0          PC 17599  71.2833   C85        C  \n",
      "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
      "3      0            113803  53.1000  C123        S  \n",
      "4      0            373450   8.0500   NaN        S  \n"
     ]
    }
   ],
   "source": [
    "print(data[data[\"Age\"]>20].head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ea550e7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
